package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.rule.ruleValue.RuleValueDTO;
import io.github.talelin.latticy.service.RuleValueService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.RuleValueDO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

/**
 * @author generator@wulongbo
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/v1/rule-value")
@Validated
@PermissionModule(value = "规则value")
public class RuleValueController {

    @Autowired
    private RuleValueService ruleValueService;


    @PostMapping("")
    @AdminRequired
    @PermissionMeta(value = "添加规则value", mount = false)
    public CreatedVO create(@RequestBody @Validated RuleValueDTO dto) {
        ruleValueService.addRuleValue(dto);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "修改规则value", mount = false)
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated RuleValueDTO validator) {
        ruleValueService.updateRuleValueById(validator,id);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "删除规则value", mount = false)
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        ruleValueService.deleteRuleValueById(id);
        return new DeletedVO();
    }

    @LoginRequired
    @GetMapping("/list/{ruleKeyId}")
    @PermissionMeta(value = "查询规则value列表", mount = false)
    public FindResultVO list(@PathVariable @Positive(message = "{id.positive}") Integer ruleKeyId) {
        return FindResultVO.success(ruleValueService.getRuleValueList(ruleKeyId));
    }

    @GetMapping("/{id}")
    @AdminRequired
    @PermissionMeta(value = "通过id查询规则value", mount = false)
    public RuleValueDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        return ruleValueService.getRuleValueById(id);
    }

    @GetMapping("/page")
    @AdminRequired
    @PermissionMeta(value = "查询所有规则value", mount = false)
    public PageResponseVO<RuleValueDO> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "ruleKeyId") Integer ruleKeyId
    ) {
        IPage<RuleValueDO> iPage = ruleValueService.getRuleValuePage(ruleKeyId,page, count);
        return PageUtil.build(iPage);
    }

}
